﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/

import ewhiteboard.display.whiteboard.WhiteBoardDisplay;
import ewhiteboard.display.whiteboard.WhiteBoardType;
import ewhiteboard.tools.whiteboard.AbstractWhiteBoardTool;
import ewhiteboard.vo.ShapeVO;

import pegas.geom.Vector2;

/**
 * This class provides the freehand whiteboard tool.
 * @author eKameleon
 */
class ewhiteboard.tools.whiteboard.FreehandTool extends AbstractWhiteBoardTool
{

	/**
	 * Creates a new FreehandTool instance.
	 * @param max The optional max number of points to draw the freehand canvas. The default value is 200.
	 * @param owner Optional reference of the WhiteBoardDisplay reference of this tool.
	 * @param bGlobal the flag to use a global event flow or a local event flow.
	 * @param sChannel the name of the global event flow if the {@code bGlobal} argument is {@code true}.
	 */	
	function FreehandTool( max:Number , owner:WhiteBoardDisplay ,  bGlobal:Boolean, sChannel:String ) 
	{
		super( owner , bGlobal, sChannel ) ;
		this.max      = max > 0 ? max : DEFAULT_MAX_POINT ;
		this.position = new Vector2(0,0) ;
	}
	
	/**
	 * The default number of points to draw a freehand canvas.
	 */
	public static var DEFAULT_MAX_POINT:Number = 200 ;
	
	/**
	 * The max points in this process.
	 */
	public var max:Number ;

	/**
	 * The position of the mouse.
	 */
	public var position:Vector2 ;
	
	/**
	 * Returns the shape value object of this tool.
	 * @return the shape value object of this tool.
	 */
	public function getShapeVO():ShapeVO
	{
		var shape:ShapeVO = new ShapeVO() ;
		shape.canvas = [].concat(_memory) ;
		shape.x      = position.x ;
		shape.y      = position.y ;
		shape.type   = getWhiteBoardType() ;
		return shape ;	
	}

	/**
	 * Returns the type of this tool.
	 * @return the type of this tool.
	 */
	public function getWhiteBoardType():Number
	{
		return WhiteBoardType.FREEHAND ;
	}

	/**
	 * Invoqued when the mouse move.
	 */
	public function move():Void 
	{
		var x:Number, y:Number ;
		if ( _cpt++ < max ) 
		{
			x = layer._xmouse ;
			y = layer._ymouse ;
			_memory.push( ['L',[x,y]] ) ;
			layer.lineTo(x,y) ;
		}
	};

	/**
	 * Reset the process.
	 */
	public function reset():Void
	{
		var c:Number = getOwner().activeColor ;
		var t:Number = getOwner().activeThickness ;
		_memory = [] ;
		_memory.push( ["S", [t, c , 100]] ) ;
		_cpt = 0 ;
		position.x = canvas._xmouse ;
		position.y = canvas._ymouse ;
		layer._x = position.x ;
		layer._y = position.y ;
		layer.lineStyle(t, c, 100) ;
	}
	
	/**
	 * Sets the owner WhiteBoardDisplay reference of this tool.
	 */	
	public function setOwner( owner:WhiteBoardDisplay ):Void
	{
		super.setOwner( owner ) ;
		canvas  = _owner.canvas ;
		layer   = _owner.layer ;
	}

	/**
	 * Stop the process.
	 */
	public function stop():Void
	{
		if ( running )
		{
			layer.clear() ;
			layer._x = 0 ;
			layer._y = 0 ;
		}
		getOwner().notifyCreate( getShapeVO() ) ;
		super.stop() ;
	}

	/**
	 * The internal count of this process.
	 */
	private var _cpt:Number = 0 ;

	/**
	 * The memory array of this process.
	 */
	private var _memory:Array ;

}